package cn.edu.zafu.bigdata.yh;
import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private static List<Student> studentList = new ArrayList<>();

    public static void main(String[] args) {
        loadStudents();
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline left-over

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    retrieveStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    running = false;
                    saveStudents();
                    System.out.println("程序已退出。");
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
            }
        }

        scanner.close();
    }

    private static void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentList = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生数据。");
        }
    }

    private static void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentList);
        } catch (IOException e) {
            System.out.println("无法保存学生数据。");
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学生学号: ");
        String id = scanner.nextLine();
        for (Student student : studentList) {
            if (student.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入学生电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        studentList.add(student);
        System.out.println("学生信息添加成功。");
        saveStudents();
    }

    private static void retrieveStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            for (Student student : studentList) {
                if (student.getId().equals(id)) {
                    System.out.println(student);
                    return;
                }
            }
            System.out.println("未找到该学生。");
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            for (Student student : studentList) {
                if (student.getId().equals(id)) {
                    System.out.print("请输入新的学生姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的学生电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    System.out.println("学生信息更新成功。");
                    saveStudents();
                    return;
                }
            }
            System.out.println("未找到该学生。");
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            boolean found = false;
            for (Iterator<Student> iterator = studentList.iterator(); iterator.hasNext(); ) {
                Student student = iterator.next();
                if (student.getId().equals(id)) {
                    iterator.remove();
                    found = true;
                    System.out.println("学生信息删除成功。");
                    saveStudents();
                    break;
                }
            }
            if (!found) {
                System.out.println("未找到该学生。");
            }
        }
    }
}
